package ui;

import entity.User;
import entity.ManagerUser;
import entity.FrontUser;
import manager.LogManager;
import manager.UserManager;
import java.util.Scanner;

public class AdminUI {
    private User admin;
    private Scanner scanner = new Scanner(System.in);
    private UserManager userManager = new UserManager();

    public AdminUI(User admin) {
        this.admin = admin;
    }

    public void showMenu() {
        while (true) {
            System.out.println("\n===== 管理员操作菜单 =====");
            System.out.println("1. 用户管理");
            System.out.println("2. 修改个人密码");
            System.out.println("3. 查看个人信息");
            System.out.println("4. 退出登录");
            System.out.print("请选择：");

            int choice = Integer.parseInt(scanner.nextLine());
            switch (choice) {
                case 1:
                    manageUsers();
                    break;
                case 2:
                    changePassword();
                    break;
                case 3:
                    admin.showUserInfo();
                    break;
                case 4:
                    System.out.println("已退出管理员账户");
                    LogManager.log("管理员" + admin.getUsername() + "退出登录", admin.getUsername());
                    return;
                default:
                    System.out.println("无效选项");
            }
        }
    }

    private void manageUsers() {
        while (true) {
            System.out.println("\n===== 用户管理 =====");
            System.out.println("1. 添加经理");
            System.out.println("2. 添加前台");
            System.out.println("3. 返回上一级");
            System.out.print("请选择：");

            int subChoice = Integer.parseInt(scanner.nextLine());
            if (subChoice == 1) {
                System.out.print("请输入经理用户名：");
                String username = scanner.nextLine();
                System.out.print("请输入初始密码：");
                String password = scanner.nextLine();
                boolean success = userManager.addUser(new ManagerUser(username, password));
                System.out.println(success ? "添加成功" : "用户名已存在");
            } else if (subChoice == 2) {
                System.out.print("请输入前台用户名：");
                String username = scanner.nextLine();
                System.out.print("请输入初始密码：");
                String password = scanner.nextLine();
                boolean success = userManager.addUser(new FrontUser(username, password));
                System.out.println(success ? "添加成功" : "用户名已存在");
            } else if (subChoice == 3) {
                break;
            } else {
                System.out.println("无效选项");
            }
        }
    }

    private void changePassword() {
        System.out.print("原密码：");
        String oldPwd = scanner.nextLine();
        System.out.print("新密码：");
        String newPwd = scanner.nextLine();
        System.out.print("确认新密码：");
        String confirmPwd = scanner.nextLine();

        if (!newPwd.equals(confirmPwd)) {
            System.out.println("两次输入不一致");
            return;
        }

        if (userManager.changePassword(admin.getUsername(), oldPwd, newPwd)) {
            System.out.println("密码修改成功");
        } else {
            System.out.println("原密码错误，修改失败");
        }
    }
}